﻿namespace dot_in.HTWKAidStation.Client
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;

    using dot_in.HTWKAidStation.Client.Mensa;
    using dot_in.HTWKAidStation.Client.Menu;
    using dot_in.HTWKAidStation.Client.Timetable;

    public static class Core
    {
        public static async Task<List<MenuWeekGroup>> ParseMenu(int mensaID, int days)
        {
            List<MenuWeekGroup> result = new List<MenuWeekGroup>();
            int offset = 0;
            for (int i = 0; i < days; i++)
            {
                if (DateTime.Now.AddDays(i).DayOfWeek == DayOfWeek.Saturday)
                {
                    offset = 2;
                }

                HttpWebRequest request =
                    WebRequest.CreateHttp(
                        "http://www.studentenwerk-leipzig.de/mensa/menu?date="
                        + DateTime.Now.AddDays(i + offset).ToString("yyyy-MM-dd") + "&location=" + mensaID);
                WebResponse response = await request.GetResponseAsync();
                StreamReader reader = new StreamReader(response.GetResponseStream());
                MenuWeekGroup tmpresult = new MenuWeekGroup(
                    DateTime.Now.AddDays(i + offset), MenuParser.GetAllMenuItems(reader.ReadToEnd()));

                if (tmpresult.MenuItems.Count == 1 && tmpresult.MenuItems[0].ItemTitle.Length == 0)
                {
                    offset++;
                    i--;
                    continue;
                }

                result.Add(tmpresult);
            }

            return result;
        }

        public static async Task<List<MenuWeekGroup>> ParseMenu(int days)
        {
            return await ParseMenu(106, days);
        }

        public static async Task<List<MenuWeekGroup>> ParseMenu()
        {
            return await ParseMenu(7);
        }

        public static async Task<List<Mensa.Mensa>> ParseMensa()
        {
            HttpWebRequest request =
                WebRequest.CreateHttp("http://www.studentenwerk-leipzig.de/mensen-und-cafeterien/speiseplan");
            WebResponse response = await request.GetResponseAsync();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            return MensaParser.GetAllMensae(reader.ReadToEnd());
        }

        public static async Task<List<EventDay>> ParseTimetable(string tutorialGroupID, string week)
        {
            HttpWebRequest request =
                WebRequest.CreateHttp(
                    "http://stundenplan.htwk-leipzig.de:8080/ws/Berichte/Text-Listen;Studenten-Sets;name;"
                    + tutorialGroupID + "?template=UNEinzelGru&weeks=_" + week);
            WebResponse response = await request.GetResponseAsync();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            return TimetableParser.GetAllEvents(reader.ReadToEnd());
        }

        public static async Task<List<Week>> ParseAllWeeks()
        {
            HttpWebRequest request =
                WebRequest.CreateHttp(@"http://stundenplan.htwk-leipzig.de:8080/stundenplan/studienjahr.xml");
            WebResponse response = await request.GetResponseAsync();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            return TimetableParser.GetAllWeeks(reader.ReadToEnd());
        }

        public static async Task<List<Faculty>> ParseAllFaculties()
        {
            HttpWebRequest request =
                WebRequest.CreateHttp(@"http://stundenplan.htwk-leipzig.de:8080/stundenplan/semgrp/semgrp_ws.xml");
            WebResponse webResponse = await request.GetResponseAsync();
            StreamReader reader = new StreamReader(webResponse.GetResponseStream());
            return TimetableParser.GetAllFaculties(reader.ReadToEnd());
        } 
    }
}
